package com.smartpower.wattmeter.ui.choose;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.basiclib.base.BaseActivity;
import com.android.basiclib.uitls.CommUtils;
import com.android.basiclib.uitls.NetWorkUtil;
import com.android.basiclib.uitls.ToastUtils;
import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.smartpower.wattmeter.R;


import java.util.ArrayList;
import java.util.List;

/**
 * 连接的设备全部的服务显示
 */
public class BLEServicesListActivity extends BaseActivity {

    private android.widget.TextView mTxtName;
    private android.widget.TextView mTxtMac;
    private android.widget.ListView listView_device;
    private ResultAdapter mResultAdapter;
    private BleDevice mBleDevice;
    private static boolean isCreated = false;

    public static void startInstance(BleDevice bleDevice) {
        if (!isCreated) {
            isCreated = true;

            Context context = CommUtils.getContext();
            Intent intent = new Intent(context, BLEServicesListActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("bleDevice", bleDevice);
            context.startActivity(intent);
        }

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_services;
    }

    @Override
    protected void getDataFromIntent(Intent intent) {
        mBleDevice = intent.getParcelableExtra("bleDevice");
    }

    @Override
    protected void init() {
        initView();
        showData();
    }

    @Override
    public void onNetworkConnectionChanged(boolean isConnected, NetWorkUtil.NetworkType networkType) {
    }

    @Override
    protected void dataObserver() {
    }

    private void initView() {
        mTxtName = findViewById(R.id.txt_name);
        mTxtMac = findViewById(R.id.txt_mac);
        listView_device = findViewById(R.id.list_service);

        mResultAdapter = new ResultAdapter(mActivity);

        listView_device.setAdapter(mResultAdapter);
        listView_device.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BluetoothGattService service = mResultAdapter.getItem(position);

                //跳转特征列表，获取全部的特征展示出来
                BLECharacterListActivity.startInstance(mBleDevice, service);
            }
        });
    }

    //展示数据
    private void showData() {

        if (mBleDevice != null) {
            String name = mBleDevice.getName();
            String mac = mBleDevice.getMac();
            BluetoothGatt gatt = BleManager.getInstance().getBluetoothGatt(mBleDevice);

            mTxtName.setText(String.valueOf("设备名称：" + name));
            mTxtMac.setText(String.valueOf("Mac地址：" + mac));

            mResultAdapter.clear();
            //获取全部的服务展示出来
            for (BluetoothGattService service : gatt.getServices()) {
                mResultAdapter.addResult(service);
            }
            mResultAdapter.notifyDataSetChanged();

        } else {
            ToastUtils.makeText(mActivity, "连接失效，已断开连接");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isCreated = false;
        BleManager.getInstance().disconnectAllDevice();
    }

    //数据适配器
    private class ResultAdapter extends BaseAdapter {

        private Context context;
        private List<BluetoothGattService> bluetoothGattServices;

        ResultAdapter(Context context) {
            this.context = context;
            bluetoothGattServices = new ArrayList<>();
        }

        void addResult(BluetoothGattService service) {
            bluetoothGattServices.add(service);
        }

        void clear() {
            bluetoothGattServices.clear();
        }

        @Override
        public int getCount() {
            return bluetoothGattServices.size();
        }

        @Override
        public BluetoothGattService getItem(int position) {
            if (position > bluetoothGattServices.size())
                return null;
            return bluetoothGattServices.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                convertView = View.inflate(context, R.layout.adapter_service, null);
                holder = new ViewHolder();
                convertView.setTag(holder);
                holder.txt_title = (TextView) convertView.findViewById(R.id.txt_title);
                holder.txt_uuid = (TextView) convertView.findViewById(R.id.txt_uuid);
                holder.txt_type = (TextView) convertView.findViewById(R.id.txt_type);
            }

            BluetoothGattService service = bluetoothGattServices.get(position);
            String uuid = service.getUuid().toString();

            holder.txt_title.setText("服务" + "(" + position + ")");
            holder.txt_uuid.setText(uuid);
            holder.txt_type.setText("服务类型(主服务)");
            return convertView;
        }

        class ViewHolder {
            TextView txt_title;
            TextView txt_uuid;
            TextView txt_type;
        }
    }


}
